package com.hongxin.nw.netty.model.analysis.info;

import com.hongxin.nw.netty.utils.FieldAnalysis;
import com.hongxin.nw.netty.values.TypeIdentif;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark A.32远程升级启动命令接收结果
 * @author: GuoXiaolin
 * @date 2019/5/28 14:14
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Getter
@Setter
public class Mrena1_16 {

    private String co;//    充电设备编号	压缩BCD码	8Byte	充电设备编号
    private String successIdentify;//2	成功标识	BIN码	1Btye	最高位：0：成功；1：失败 低7位：失败原因 1:SM4密钥错误  127:其它原因

    private int idx = 0;
    private ByteBuf byteBuf;

    public Mrena1_16 analysisStr(int idx, ByteBuf byteBuf) {
        this.idx = idx;
        this.byteBuf = byteBuf;

        return this.subCo().subSuccessIdentify();
    }

    private Mrena1_16 subCo() {
//        this.co =  substring(TypeIdentif.BYTE_CO.getbyteLength());
//        this.co = FieldAnalysis.analysisBCD(this.co);
//        return this;

//        int byteLength = TypeIdentif.BYTE_CO.getInt();
//        this.co = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
//        this.co = FieldAnalysis.analysisBCD(this.co);
//        this.idx += byteLength;

        this.co = FieldAnalysis.subCo(TypeIdentif.BYTE_CO.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_CO.getInt();
        return this;
    }

    private Mrena1_16 subSuccessIdentify() {
//        this.successIdentify =  substring(TypeIdentif.BYTE_SUCCESS_IDENTIFY.getbyteLength());
//        this.successIdentify = FieldAnalysis.analysisBin(this.successIdentify);
//        this.successIdentify = String.valueOf(Integer.toBinaryString(Integer.parseInt(this.successIdentify)).charAt(this.successIdentify.length()-1));
//        return this;

//        int byteLength = TypeIdentif.BYTE_SUCCESS_IDENTIFY.getInt();
//        this.successIdentify = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
//        this.successIdentify = FieldAnalysis.analysisBin(this.successIdentify);
//        this.successIdentify = String.valueOf(Integer.toBinaryString(Integer.parseInt(this.successIdentify)).charAt(this.successIdentify.length()-1));
//        this.idx += byteLength;

        this.successIdentify = FieldAnalysis.subSuccess2(TypeIdentif.BYTE_SUCCESS_IDENTIFY.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_SUCCESS_IDENTIFY.getInt();
        return this;
    }


    @Override
    public String toString() {
        return "Mrena1_16{" +
                "co='" + co + '\'' +
                ", successIdentify='" + successIdentify + '\'' +
                '}';
    }

}
